using System.Collections;
using System.Collections.Generic;
using FrameWork;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace FrameWork.Inventory
{
    public class InventoryView : MonoBehaviour
    {
        [Header("图标挂载父物体")]
        [SerializeField] private GameObject objUI;
        
        
        [SerializeField] private Slot slotPrefab;

        private List<Slot> slots = new List<Slot>();
        
        // 更新视图
        public void UpdateViewInfo(int index, InventoryItem item)
        {
            slots[index].SetSlot(item);
        }

        public void ResetAllItems()
        {
            foreach (var VARIABLE in slots)
            {
                VARIABLE.ResetSlot();
            }
        }

        // 填充所有物品格子
        public void FillItemToUI(int size)
        {
            ClearChild();
            for (int i = 0; i < size; i++)
            {
                Slot slot = Instantiate(slotPrefab, Vector3.zero, Quaternion.identity);
                slot.transform.SetParent(objUI.transform);
                slot.ResetSlot();
                slots.Add(slot);
            }
        }

        // 清空子物体
        private void ClearChild()
        {
            foreach (Transform child in objUI.transform)
            {
                // 销毁子对象，包括其所有组件
                Destroy(child.gameObject);
            }
        }
    }
}
